package com.java2007.hotel.service.impl;

import com.java2007.hotel.dao.OrderDao;
import com.java2007.hotel.dao.OrderDetailDao;
import com.java2007.hotel.dao.impl.OrderDaoImpl;
import com.java2007.hotel.dao.impl.OrderDetailDaoImpl;
import com.java2007.hotel.entity.DinnerCar;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.pojo.OrderDetail;
import com.java2007.hotel.service.OrderService;
import com.java2007.hotel.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/28
 **/
public class OrderServiceImpl implements OrderService {
    private OrderDao orderDao = new OrderDaoImpl();
    private OrderDetailDao orderDetailDao = new OrderDetailDaoImpl();

    /**
     * 添加订单（订单和详细订单）
     * @param dinnerCarList
     * @param totalPrice
     * @param tableId
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> addOrder(List<DinnerCar> dinnerCarList, Long totalPrice, Integer tableId, Integer userId) throws Exception {
        //用于存储返回值
        Map<String,Object> map = new HashMap<>();



        //封装订单
        Order order = new Order();
        order.setOrderCteateUserId(userId);
        order.setOrderTableId(tableId);
        order.setOrderCreateTime(new Date());
        order.setOrderUpdateTime(new Date());
        order.setOrderId(System.currentTimeMillis());
        order.setOrderPayStatus(0);
        order.setOrderPrice(totalPrice);
        //将订单对象存进map中
        map.put("order",order);

        try {
            JdbcUtils.startTransactions();
            orderDao.addOrder(order);

            //从购物车内遍历出所有订单详情信息
            OrderDetail orderDetail = new OrderDetail();
            List<OrderDetail> orderDetailsList = new ArrayList<>();
            for (DinnerCar dinnerCar : dinnerCarList) {
                orderDetail = dinnerCar;
                orderDetail.setDetailId(System.currentTimeMillis());
                orderDetail.setDetailCreateTime(new Date());
                orderDetail.setDetailOrderId(order.getOrderId());
                orderDetail.setDetailUpdateTime(new Date());

                //停顿20ms防止存储数据库失败
                Thread.sleep(20);
                orderDetailDao.addorderDetail(orderDetail);
                //将详细订单存储到列表中
                orderDetailsList.add(orderDetail);
            }

            //将详细订单列表存进map中
            map.put("orderDetailsList",orderDetailsList);

            JdbcUtils.commit();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return null;
        }
    }


    /**
     * 修改订单
     * @return
     */
    @Override
    public int updateOrder(Order order) {
        try {
            JdbcUtils.startTransactions();
            orderDao.updateOrder(order);
            JdbcUtils.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return 0;
        }
    }

    /**
     * 查询所有订单
     * @return
     */
    @Override
    public List<Order> findAllOrder() throws SQLException {
        return orderDao.findAllOrder();
    }

    /**
     * 通过id查询一条订单
     * @param orderId
     * @return
     */
    @Override
    public Order findOrder(Long orderId) throws SQLException {

        return orderDao.findOrder(orderId);
    }

    /**
     * 通过用户id查询用户下的订单
     * @param userId
     * @return
     */
    @Override
    public List<Order> findOrderByCreatUserId(Integer userId) throws SQLException {
        return orderDao.findOrderByCreatUserId(userId);
    }


}
